"""
Author        yongfa
Date          2022-01-28 11:30:02
LastEditTime  2022-07-21 14:00:18
LastEditors   yongfa
Description   任务模板
"""
from flask import current_app
from app.extensions import celery_app
from app.modules.v1.system_logs.models import SystemLog


# 普通测试
@celery_app.task()
def add_together(a, b):
    try:
        print('sdfsdfsfs')
        current_app.logger.error("celery success！")
        logs = SystemLog.get_all()
        print(logs)
        return a + b
    except Exception as e:
        current_app.logger.exception("celery fail！ error: {}".format(str(e)))


# 定时测试
@celery_app.task()
def crontab_test():
    try:
        current_app.logger.error("celery success！ 我是定时任务！")
    except Exception as e:
        current_app.logger.exception("celery fail！ error: {}".format(str(e)))


# 失败重试
@celery_app.task(bind=True)
def test_retry(self, x, y):
    """重试任务"""
    try:
        import time
        time.sleep(10)
        current_app.logger.error("celery success！")
        return x + y
    except Exception as exc:
        current_app.logger.exception("celery fail！ error: {}".format(str(exc)))
        raise self.retry(countdown=5, max_retries=3, exc=exc)  # 下次重试5s以后，最多重试3次
